package pl.edu.pjwstk.kor.sbql4j.project3;

import pl.wcislo.sbql4j.db4o.Db4oSBQLQuery;
import com.db4o.ObjectContainer;
import pl.edu.pjwstk.kor.database.Database;
import pl.edu.pjwstk.kor.database.Sbql4jQuery;
import pl.edu.pjwstk.kor.model.Employee;
import pl.edu.pjwstk.kor.model.Rank;
import pl.edu.pjwstk.kor.model.Service;
import pl.wcislo.sbql4j.java.model.runtime.Struct;
import java.util.Collection;

/**
 * union--
 * minus--
 * intersect --
 * in--
 * sum - ju\u017c by\u0142y
 * avg - ju\u017c by\u0142y
 * min - ju\u017c by\u0142y
 * max - ju\u017c by\u0142y
 * count--
 */
public class Project3Query implements Sbql4jQuery {
    
    public Project3Query() {
        super();
    }
    private Database database;
    
    @Override()
    public void setDatabase(Database database) {
        this.database = database;
    }
    
    /**
     * suma wszystkich sprzedawców i managerów
     * Object res = #{
     *		  db.(Employee where job == "sprzedawca")
     *	union db.(Employee where job == "manager")
     * count union
     */
    public int query1() {
        ObjectContainer db = database.getObjectContainer();
        Object res = new Project3Query_SbqlQuery0(db).executeQuery();
        return (Integer)res;
    }
    
    /**
     * #{ db.(Employee where boss.lastName in(names)) };
     * @return
     */
    public Collection<Employee> query2() {
        ObjectContainer db = database.getObjectContainer();
        String[] names = new String[]{"Sikora", "Olszewska"};
        Object res = new Project3Query_SbqlQuery1(db,names).executeQuery();
        return (Collection<Employee>)res;
    }
    
    /**
     * #{ db.(Employee where job in(jobs)) intersect db.(Employee where boss.lastName in(names)) }
     * sprzedawcy i managery których szefem jest sikora albo olszewska
     * @return
     */
    public Collection<Employee> query3() {
        ObjectContainer db = database.getObjectContainer();
        String[] jobs = new String[]{"sprzedawca", "manager"};
        String[] names = new String[]{"Sikora", "Olszewska"};
        Object res = new Project3Query_SbqlQuery2(db,jobs,names).executeQuery();
        return (Collection<Employee>)res;
    }
    
    /**
     * #{ db.(Employee where job in(jobs)) minus db.(Employee where count(deals) > 2) };
     * sprzedawcy którzy zawarli mniej ni\u017c 2 umowy
     * @return
     */
    public Collection<Employee> query4() {
        ObjectContainer db = database.getObjectContainer();
        String[] jobs = new String[]{"sprzedawca"};
        Object res = new Project3Query_SbqlQuery3(db,jobs).executeQuery();
        return (Collection<Employee>)res;
    }
    
    /**
     * #{ db.(min(Service.minServiceCost) as minimum, avg(Service.minServiceCost) as average, max(Service.maxServiceCost) as maximum )};
     * @return
     */
    public Struct query5() {
        ObjectContainer db = database.getObjectContainer();
        Object res = new Project3Query_SbqlQuery4(db).executeQuery();
        Struct s = (Struct)res;
        return s;
    }
    
    /**
     * #{ db.(Employee where avg(deals.cost) < (avg(Employee.deals.cost))) };
     * @return
     */
    public Collection<Employee> query6() {
        ObjectContainer db = database.getObjectContainer();
        String[] jobs = new String[]{"sprzedawca", "manager"};
        Object res = new Project3Query_SbqlQuery5(db).executeQuery();
        return (Collection<Employee>)res;
    }
}